//
//  GenderPickerView.m
//  ShopStore
//
//  Created by 东亨 on 2017/5/9.
//  Copyright © 2017年 lichao. All rights reserved.
//

#import "GenderPickerView.h"
#import "UIViewExt.h"
#import "PropertyListModel.h"
#define navigationViewHeight 44.0f
#define pickViewViewHeight 150.0f
#define buttonWidth 60.0f

#define windowColor  [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2]
#define kScreenWidth  [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
@interface GenderPickerView ()

@property(nonatomic,strong)NSMutableArray *genderArray;//物业公司列表

@property(nonatomic,strong)UIView *bottomView;//包括导航视图和地址选择视图
@property(nonatomic,strong)UIPickerView *pickView;//地址选择视图
@property(nonatomic,strong)UIView *navigationView;//上面的导航视图
@property(assign) int indexArea;
@property(assign) int indexlocality;
@property(assign) int indexsubLocality;
@property(nonatomic,strong)NSMutableDictionary  *dataDic;
@end


@implementation GenderPickerView

//+ (instancetype)shareInstance
//{
//    static GenderPickerView *shareInstance = nil;
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        shareInstance = [[GenderPickerView alloc] init];
//    });
//    
//    [shareInstance showBottomView];
//    return shareInstance;
//}
-(instancetype)initWithArr:(NSArray *)typeArr
{
    self = [super init];
    if (self) {
        _genderArray = [[NSMutableArray alloc]initWithCapacity:0];
        self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        [self _addTapGestureRecognizerToSelf];
        for (PropertyListModel *model in typeArr)
        {
            [_genderArray addObject:model.property_name];
        }
        [self _createView];
    }
    return self;
}
#pragma mark - get data

-(void)_addTapGestureRecognizerToSelf
{
    self.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hiddenBottomView)];
    [self addGestureRecognizer:tap];
}
-(void)_createView
{
    _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, navigationViewHeight+pickViewViewHeight)];
    [self addSubview:_bottomView];
    //导航视图
    _navigationView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, navigationViewHeight)];
    _navigationView.backgroundColor = RGB(245, 245, 245);
    [_bottomView addSubview:_navigationView];
    //这里添加空手势不然点击navigationView也会隐藏,
    UITapGestureRecognizer *tapNavigationView = [[UITapGestureRecognizer alloc]initWithTarget:self action:nil];
    [_navigationView addGestureRecognizer:tapNavigationView];
    NSArray *buttonTitleArray = @[@"取消",@"确定"];
    for (int i = 0; i <buttonTitleArray.count ; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(i*(kScreenWidth-buttonWidth), 0, buttonWidth, navigationViewHeight);
        button.titleLabel.font  = HYQIHEI_Regular(14);
        [button setTitleColor:[UIColor colorWithHex:0xff0000] forState:UIControlStateNormal];
        [button setTitle:buttonTitleArray[i] forState:UIControlStateNormal];
        [_navigationView addSubview:button];
        button.tag = i;
        [button addTarget:self action:@selector(tapButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    _pickView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, navigationViewHeight, kScreenWidth, pickViewViewHeight)];
    _pickView.backgroundColor = [UIColor whiteColor];
    _pickView.dataSource = self;
    _pickView.delegate =self;
    [_pickView selectRow:_indexArea inComponent:0 animated:YES];
    [_bottomView addSubview:_pickView];
}
-(void)tapButton:(UIButton*)button
{//点击确定回调block
    if (button.tag == 1) {

        NSString *gender = [self.genderArray objectAtIndex:[_pickView selectedRowInComponent:0]];
        _block(gender);
    }
    [self hiddenBottomView];
}
-(void)showBottomView
{
    self.backgroundColor = [UIColor clearColor];
    [UIView animateWithDuration:0.3 animations:^{
        
        _bottomView.top = kScreenHeight-navigationViewHeight-pickViewViewHeight;
        self.backgroundColor = windowColor;
    } completion:^(BOOL finished) {
        
    }];
}
-(void)hiddenBottomView
{
    [UIView animateWithDuration:0.3 animations:^{
        _bottomView.top = kScreenHeight;
        self.backgroundColor = [UIColor clearColor];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
#pragma mark - UIPicker Delegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.genderArray.count;
}
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel *lable=[[UILabel alloc]init];
    lable.textAlignment=NSTextAlignmentCenter;
    lable.font= HYQIHEI_Regular(14);
    lable.text=[self.genderArray objectAtIndex:row];
    return lable;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    CGFloat pickViewWidth = kScreenWidth/3;
    
    return pickViewWidth;
}
@end
